﻿using System;
using System.Windows.Data;

namespace Adder
{
    /// <summary>
    /// Simply add a parameter to value
    /// </summary>
    public class Adder : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return DoConvert(value, targetType, parameter, culture);
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return DoConvert(value, targetType, parameter, culture);
        }


        object DoConvert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (targetType == typeof(double))
            {
                double rez;
                if (double.TryParse(parameter.ToString(), out rez))
                {
                    return (double)((double)value + rez);
                }
                else
                    return (double)value;
            }

            if (targetType == typeof(int))
            {
                int rez;
                if (int.TryParse(parameter.ToString(), out rez))
                {
                    return (int)((int)value + rez);
                }
                else
                    return (int)value;
            }

            if (targetType == typeof(string))
            {
                return (string)(parameter.ToString() + value.ToString());
            }

            if (targetType == typeof(bool))
            {
                return !((bool)value);
            }
            throw new NotImplementedException();

        }
    }
}
